/**
 * Created by lijtg on 2015/10/21.
 */
$(document).ready(function () {
    var main = {
        config: {
        },
        cache: {
            timer: null
        },
        startTimer: function () {
            var btn = $('#pr-btn-msgCode');
            var time = 60;
            btn.attr('disabled', 'disabled');
            btn.html('重新发送(' + time + 'S)');
            main.cache.timer = setInterval(function () {
                if(time < 0){
                    btn.attr('disabled', false);
                    btn.html('重新发送');
                    clearInterval(main.cache.timer);
                    return;
                }
                btn.html('重新发送(' + time-- + 'S)');
            }, 1000);
        },
        sendMsgCode: function (){
            var btn = $('#pr-btn-msgCode');
            if(btn.attr('disabled') == 'disabled') return;
            var phone = '';
            $.ajax({
                type: "POST",
                url: common.hostPath + "/api/sendMsgCode",
                data: {
                    phone: phone
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {
                        main.startTimer();
                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){
                    main.startTimer();
                },
                error: common.responseError
            });
        },
        getAcctInfo: function (){
            $.ajax({
                type: "GET",
                url: common.hostPath + "/api/accountQuery",
                data: {
                    IDENTIFICATION_TYPE: '101',
                    IDENTIFICATION_NUMBER: '',
                    CUSTOMER_NAME: '',
                    BASE_ACCT_NO: ''
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {

                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){

                },
                error: common.responseError
            });
        },
        getBindAcctInfo: function (){
            $.ajax({
                type: "GET",
                url: common.hostPath + "/api/bindAccountQuery",
                data: {
                    IDENTIFICATION_TYPE: '101',
                    IDENTIFICATION_NUMBER: '',
                    CUSTOMER_NAME: '',
                    BASE_ACCT_NO: ''
                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {

                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){

                },
                error: common.responseError
            });
        },
        submit: function (){
            var btn = $('#pr-btn-submit');
            if(btn.attr('disabled') == 'disabled') return;
            if(!main.formCheck()) return;
            $.ajax({
                type: "POST",
                url: common.hostPath + "/api/register",
                data: {

                },
                dataType : 'json',
                success: function (data) {
                    if (data.errCode == "000000") {

                    }
                    else {
                        common.responseFail(data)
                    }
                },
                complete: function(){
                    btn.attr('disabled', false);
                    layer.open({
                        content: '提交成功',
                        btn: '确定'
                    });
                },
                error: common.responseError
            });
        },
        formCheck: function () {
            var docId = $('#pr-docId').val();
            if($.trim(docId) == '') {
                common.alertLayer({
                    errMsg: '身份证号码有误'
                });
                return false;
            }

            var phone = $('#pr-phone').val();
            if(!common.isMobile(phone)) {
                common.alertLayer({
                    errMsg: '联系人手机号有误'
                });
                return false;
            }

            var transPwdOld = $.trim($('#pr-transPwd-old').val());
            if (isNaN(transPwdOld) || transPwdOld.length != 6) {
                common.alertLayer({
                    errMsg: '原支付密码格式有误'
                });
                return false;
            }

            var transPwd = $.trim($('#pr-transPwd').val());
            if (isNaN(transPwd) || transPwd.length != 6) {
                common.alertLayer({
                    errMsg: '新支付密码格式有误'
                });
                return false;
            }

            var msgCode = $('#pr-msgCode').val();
            if($.trim(msgCode) == '') {
                common.alertLayer({
                    errMsg: '验证码有误'
                });
                return false;
            }

            return true;
        },
        regHammer: function () {
            var hammer2 = new Hammer($('#pr-btn-msgCode')[0]);
            hammer2.on('tap', function (ev) {
                main.sendMsgCode();
            });
            var hammer4 = new Hammer($('#pr-btn-submit')[0]);
            hammer4.on('tap', function (ev) {
                main.submit();
            });
        },
        init: function () {
            main.regHammer();
        }
    };

    main.init();
});